<template>
	<view class="goods-detail-container">
		<!-- <text>详情页面</text> -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" class="swiper-goods-detail">
			<swiper-item v-for="(item,index) in goodsInfo.pics" :key='index' class="swiper-goods-detail-item" >
				<image :src="item.pics_big" mode="" class="swiper-goods-detail-item-img" @click="preview(index)"></image>
			</swiper-item>
		</swiper>
		
		<view class="goods-info-box" v-if="goodsInfo.goods_name">
			<view class="price">￥{{goodsInfo.goods_price}}</view>
			<view class="goods-body">
				<view class="goods-name">
					{{goodsInfo.goods_name}}
				</view>
				<view class="shoucang">
					<uni-icons type="star" size='30' color="red"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yunfei">快递：免运费</view>
		</view>
		
		<rich-text class="rich-box" :nodes="goodsInfo.goods_introduce"></rich-text>
		
		<view class="goods-nav">
			<uni-goods-nav @click="iconClick" @buttonClick="buttonClick" :fill="true" :options='options' :buttonGroup="buttonGroup"></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import {goodsDetailData} from '@/api/goods-list-request.js';
	import {mapMutations,mapGetters} from 'vuex';
	export default {
		computed:{
			...mapGetters('cart_m',['total'])
		},
		data() {
			return {
				goodsInfo:{},
				options:[
					{
						icon:'shop',
						text:'商店'
					},
					{
						icon:'cart',
						text:'购物车',
						info:99
					} 
				],
				buttonGroup:[
					{
						text:'加入购物车',
						backgroundColor:'red',
						color:'#fff'
					},
					{
						text:'立即购买',
						backgroundColor:'orange',
						color:'#fff'
					}
				],
			};
		},
		onLoad(options){
			console.log(options);
			this.getGoodsInfo(options.goods_id)
		},
		methods:{
			...mapMutations('cart_m',['addToCart']),
			async getGoodsInfo(goods_id){
				const {data:res} = await goodsDetailData(goods_id);
				console.log(res);
				
				if(res.meta.status !== 200){
					return uni.showToast({
						title:'数据请求失败，请稍后',
						icon:'none'
					})
				};
				
				// goods_introduce
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g,'<img style"display:block;" ').replace(/webp/g,'jpg');
				this.goodsInfo = res.message;
			},
			
			preview(index){
				uni.previewImage({
					current:index,
					urls:this.goodsInfo.pics.map(item=>item.pics_big)
				})
			},
			iconClick(e){
				console.log('左侧');
				console.log(e);
				if(e.content.text === '购物车'){
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
				
			},
			buttonClick(e){
				console.log('右侧');
				console.log(e);
				if(e.content.text === '加入购物车'){
					const goods = {
						goods_id:this.goodsInfo.goods_id,
						goods_name:this.goodsInfo.goods_name,
						goods_price:this.goodsInfo.goods_price,
						goods_count:1,
						goods_small_logo:this.goodsInfo.goods_small_logo,
						goods_state:true
					};
					this.addToCart(goods);
					// const findResult =  this.options.find(item=>item.text == '购物车');
					// if(findResult){
					// 	findResult.info = this.total
					// }
				}
			}
		},
		watch:{
			total:{
				handler(newValue){
					console.log(newValue);
					const findResult =  this.options.find(item=>item.text == '购物车');
					if(findResult){
						findResult.info = newValue
					}
				},
				immediate:true
			}
		}
	}
</script>

<style lang="scss">
	
	.goods-detail-container{
		padding-bottom: 50px;
		.swiper-goods-detail{
			height: 750rpx;
			.swiper-goods-detail-item {
				.swiper-goods-detail-item-img {
					width: 100%;
					height: 100%;
				}
			}
		}
		.goods-info-box{
			padding: 5px;
			.price {
				color: #2979FF;
				font-size: 30px;
			}
			.goods-body{
				display: flex;
				.goods-name {
					font-size: 15px;
					margin-right: 5px;
					// border-right: 1px solid #3A3A3A;
				}
				.shoucang {
					width: 100px;
					font-size: 10px;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					border-left: 1px solid #3A3A3A;
				}
			}
			.yunfei {
				color: #303133;
				font-size: 10px;
				margin: 10px 0;
			}
		}
		.goods-nav{
			position: fixed;
			left: 0;
			bottom: 0;
			width: 100%;
		}
	}
	.rich-box{
		height: 100%;
		width: 100%;
	}
	

</style>
